
package portlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import util.ConfigUtil;
import corewa.CoreWa;
import event.Event;
import form.BaseForm;
import form.Contribute;

/**
 * OnlineDictPortlet Portlet Class
 * @author disable
 */
public class OnlineDictPortlet extends GenericPortlet {
	CoreWa coreWebAppCfg = null;
	String nextScr = null;
	BaseForm result = null;
	String contextPath=null;
	Logger LOG = Logger.getLogger("OnlineDictPortlet");
	@Override
	public void init(PortletConfig portletCfg) throws PortletException {
		// TODO Auto-generated method stub
		super.init(portletCfg);
		contextPath = portletCfg.getInitParameter("contextPath");
		String mvcPortletCfg = portletCfg.getInitParameter("OnlineDictConfig");
		String realPathCfg = portletCfg.getPortletContext().getRealPath(mvcPortletCfg);
		coreWebAppCfg = ConfigUtil.parse(realPathCfg);
	}


	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		PortletRequestDispatcher dispatcher = null;
		request.getPortletSession().setAttribute("messError",
				"",
				PortletSession.APPLICATION_SCOPE);
		
		if(nextScr == null) {
			request.getPortletSession().setAttribute("username", "Guest", PortletSession.APPLICATION_SCOPE);
			request.getPortletSession().setAttribute("userrole", -1, PortletSession.APPLICATION_SCOPE);
			dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/S01_LookUpWord.jsp");
		}
		else {
			if(result instanceof form.LoginForm) {
				form.LoginForm loginForm = (form.LoginForm)result;
				String username = loginForm.getLoginUsername();
				int role = loginForm.getRole();
				if(role == -1) {
					//login fail
					request.getPortletSession().setAttribute("messError",
							"Invalid user and password",
							PortletSession.APPLICATION_SCOPE);
					nextScr = "/WEB-INF/jsp/S03_Login.jsp";
					request.setAttribute("result", null);
				}
				if(role == 0) {
					//user
					request.getPortletSession().setAttribute("username", username, PortletSession.APPLICATION_SCOPE);
					request.getPortletSession().setAttribute("userrole", role, PortletSession.APPLICATION_SCOPE);
					nextScr = "/WEB-INF/jsp/S01_LookUpWord.jsp";
					request.setAttribute("result", null);
				}
				if(role == 1 || role == 2) {
					//manager
					request.getPortletSession().setAttribute("username", username, PortletSession.APPLICATION_SCOPE);
					request.getPortletSession().setAttribute("userrole", role, PortletSession.APPLICATION_SCOPE);
					request.setAttribute("result", null);
				}
			}
			if(result instanceof form.SearchWord) {
				request.setAttribute("result", result);
			}
			if(result instanceof form.Contribute) {
				form.Contribute contribute = (Contribute)result;
				if(contribute.getSuccess() == 1) {
					request.getPortletSession().setAttribute("message", "Thank you for your contribute."
							+"<br>The word you suggest is waiting for approving."
							+"<br>We will email you as soon as.", PortletSession.APPLICATION_SCOPE);
				}
				else {
					request.getPortletSession().setAttribute("message", "We can not insert your suggested word"
							+"<br>It may be caused error system."
							+"<br>Please try again later.", PortletSession.APPLICATION_SCOPE);
				}
				request.setAttribute("result", null);
			}
			LOG.info(nextScr);
			dispatcher = getPortletContext().getRequestDispatcher(nextScr);
		}
		
		request.setAttribute("contextPath", contextPath);
	    dispatcher.include(request, response);
		
	}


	public void doEdit(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		
        PortletRequestDispatcher dispatcher =
	        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/OnlineDictPortlet_edit.jsp");
        dispatcher.include(request, response);
		
	}

	public void doHelp(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		
        PortletRequestDispatcher dispatcher =
	        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/OnlineDictPortlet_help.jsp");
        dispatcher.include(request, response);
		
	}

	public void processAction(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		String screenId = request.getParameter("screenId");
        String eventId = request.getParameter("eventId");
        screen.Screen screen = coreWebAppCfg.getScreen(screenId);
        Event event = screen.getEvent(eventId);
        String procId = null;
        if(event != null) {
        	procId = event.getProcId();
        	nextScr = event.getNextScrId();
        }
        try {
            Method method;
            Object controlClass = Class.forName(screen.getCtrlClass()).newInstance();
            method = controlClass.getClass().getMethod(procId, ActionRequest.class, ActionResponse.class);
            result = (BaseForm) method.invoke(controlClass, request, response);
        } catch (Exception ex) {
        }
		
	}

}
